package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.Repair;
import com.amsp.oa.oademo.mapper.RepairMapper;
import com.amsp.oa.oademo.service.RepairService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Random;
import java.text.SimpleDateFormat;

@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Override
    public IPage<Repair> getRepairList(Page<Repair> page, String keyword, String status) {
        LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(keyword)) {
            wrapper.like(Repair::getRepairNo, keyword)
                  .or()
                  .like(Repair::getApplicant, keyword)
                  .or()
                  .like(Repair::getPhone, keyword);
        }
        
        if (StringUtils.hasText(status)) {
            wrapper.eq(Repair::getStatus, status);
        }
        
        wrapper.orderByDesc(Repair::getCreateTime);
        
        return this.page(page, wrapper);
    }

    @Override
    public Repair submitRepair(Repair repair) {
        // 1. 生成报修编号
        String repairNo = generateRepairNo();
        repair.setRepairNo(repairNo);
        
        // 2. 设置初始状态
        repair.setStatus("pending");
        
        // 3. 设置时间
        Date now = new Date();
        repair.setCreateTime(now);
        repair.setUpdateTime(now);
        
        // 4. 数据验证
        validateRepair(repair);
        
        // 5. 保存数据
        this.save(repair);
        
        return repair;
    }

    @Override
    public Repair updateRepair(Repair repair) {
        repair.setUpdateTime(new Date());
        this.updateById(repair);
        return repair;
    }

    @Override
    public void deleteRepair(Long id) {
        this.removeById(id);
    }

    @Override
    public Repair getRepairByNo(String repairNo) {
        LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Repair::getRepairNo, repairNo);
        return this.getOne(wrapper);
    }

    @Override
    public IPage<Repair> getMyRepairs(Page<Repair> page, String applicant) {
        LambdaQueryWrapper<Repair> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Repair::getApplicant, applicant)
              .orderByDesc(Repair::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public Repair handleRepair(Long id, String handlerName, String handleResult) {
        Repair repair = this.getById(id);
        repair.setHandlerName(handlerName);
        repair.setHandleResult(handleResult);
        repair.setHandleTime(new Date());
        repair.setStatus("completed");
        repair.setUpdateTime(new Date());
        this.updateById(repair);
        return repair;
    }

    @Override
    public Repair cancelRepair(Long id) {
        Repair repair = this.getById(id);
        repair.setStatus("cancelled");
        repair.setUpdateTime(new Date());
        this.updateById(repair);
        return repair;
    }

    private String generateRepairNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        String random = String.format("%03d", new Random().nextInt(1000));
        return "BX" + dateStr + random;
    }

    private void validateRepair(Repair repair) {
        if (StringUtils.isEmpty(repair.getApplicant())) {
            throw new IllegalArgumentException("报修人不能为空");
        }
        if (StringUtils.isEmpty(repair.getPhone())) {
            throw new IllegalArgumentException("联系电话不能为空");
        }
        if (StringUtils.isEmpty(repair.getLocationBuilding()) 
            || StringUtils.isEmpty(repair.getLocationFloor())
            || StringUtils.isEmpty(repair.getLocationRoom())) {
            throw new IllegalArgumentException("报修位置不能为空");
        }
        if (StringUtils.isEmpty(repair.getRepairType())) {
            throw new IllegalArgumentException("报修类型不能为空");
        }
        if (StringUtils.isEmpty(repair.getDescription())) {
            throw new IllegalArgumentException("故障描述不能为空");
        }
        if (repair.getExpectedStartTime() == null || repair.getExpectedEndTime() == null) {
            throw new IllegalArgumentException("期望维修时间不能为空");
        }
        if (repair.getExpectedEndTime().before(repair.getExpectedStartTime())) {
            throw new IllegalArgumentException("期望结束时间不能早于开始时间");
        }
    }
} 